package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.QsDicDao;
import com.djinfo.work.dao.QsQuestionDao;
import com.djinfo.work.entity.QsDicEntity;
import com.djinfo.work.entity.QsQuestionEntity;
import com.djinfo.work.service.QsQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;




@Service("qsQuestionService")
public class QsQuestionServiceImpl implements QsQuestionService {
	@Autowired
	private QsQuestionDao questionDao;
	@Override
	public void saveOrUpdate(QsQuestionEntity object,String uesrId) {
		if(object.getId()==null){
			object.setCreateTime(StringUtil.getNowTime());
			object.setUpdateTime(StringUtil.getNowTime());
			object.setCteateUser(uesrId);
			object.setUpdateUser(uesrId);
			questionDao.saveOrUpdate(object);
		}else{
			QsQuestionEntity qs = questionDao.getById(object.getId());
			qs.setUpdateUser(uesrId);
			qs.setUpdateTime(StringUtil.getNowTime());
			qs.setDescription(object.getDescription());
			qs.setName(object.getName());
			questionDao.saveOrUpdate(qs);
		}

	}

	@Override
	public HashMap<String, Object> getPage(QsQuestionEntity object, Integer pageNum, Integer pageSize) {
		HashMap<String, Object> map = new HashMap<>();
		AjaxJson ajaxJson = new AjaxJson();
		List<QsQuestionEntity> res = page(object,pageNum,pageSize);
		Integer count = getAllRowCount(object);
		map.put("dataList",res);
		map.put("total",count);
		return map;
	}

	@Override
	public List<QsQuestionEntity> page(QsQuestionEntity tbDicNews, Integer pageNum, Integer pageSize) {
		String hql = " from QsQuestionEntity where 1=1 ";
		if(tbDicNews.getName()!=null && !"".equals(tbDicNews.getName())){
			hql+= " and name like '%" + tbDicNews.getName() + "%' or description like '%" + tbDicNews.getName() + "%'";
		}
		return questionDao.page( hql,  pageNum,  pageSize);
	}

	@Override
	public List<QsQuestionEntity> list(QsQuestionEntity object) {
		return questionDao.list(object);
	}

	@Override
	public Integer getAllRowCount(QsQuestionEntity object) {
		String hql = " select count(*) from QsQuestionEntity where 1=1 ";
		if(object.getName()!=null && !"".equals(object.getName())){
			hql+= " and name like '%" + object.getName() + "%' or description like '%" + object.getName() + "%'";
		}
		return questionDao.getAllRowCount(hql);
	}


	@Override
	public void delete(QsQuestionEntity object) {
		questionDao.delete(object);
	}

	@Override
	public QsQuestionEntity selectOne(QsQuestionEntity object) {
		return questionDao.selectOne( object);
	}

	@Override
	public QsQuestionEntity getById(String id) {
		return questionDao.getById(id);
	}
}
